package org.edu.modules.stu.shet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.utils.ConvertUtils;
import org.edu.common.base.BaseServiceImpl;
import org.edu.modules.stu.shet.entity.VeStuAssocCharge;
import org.edu.modules.stu.shet.mapper.VeStuAssocInfoMapper;
import org.edu.modules.stu.shet.entity.VeStuAssocInfo;
import org.edu.modules.stu.shet.dto.VeStuAssocInfoDto;
import org.edu.modules.stu.shet.service.VeStuAssocChargeService;
import org.edu.modules.stu.shet.service.VeStuAssocInfoService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



/**
 * 实现类
 */
@Service
public class VeStuAssocInfoServiceImpl extends BaseServiceImpl<VeStuAssocInfo, VeStuAssocInfoMapper> implements VeStuAssocInfoService {

    @Autowired
    private VeStuAssocChargeService veStuAssocChargeService;

    public IPage<VeStuAssocInfoDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper){
        return mapper.pageDto(page,queryWrapper);
    }
    /**
    * 查询实体类
    */
    public VeStuAssocInfoDto findDtoById(String id) {
        VeStuAssocInfo entity = super.findById(id);
        VeStuAssocInfoDto dto = ConvertUtils.convertToDto(entity, VeStuAssocInfoDto.class);


        LambdaQueryWrapper<VeStuAssocCharge> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(VeStuAssocCharge::getStuId);
        lambdaQueryWrapper.eq(VeStuAssocCharge::getAssocId,dto.getId());
        List<VeStuAssocCharge> veJkyOrganizeUsers = veStuAssocChargeService.listByWrapper(lambdaQueryWrapper);
        List<String> userIds = new ArrayList<>();
        for (VeStuAssocCharge user: veJkyOrganizeUsers) {
            userIds.add(user.getStuId()+"");
        }
        dto.setUserIds(userIds);
        
        
        return dto;
    }

    /**
    * 新增或者修改
    */
    @Transactional
    public void insertOrUpdate(VeStuAssocInfoDto dto,Boolean isAdd){
        if(isAdd){
            super.insert(dto);
        }else{
            super.update(dto);
        }


        for (String userId:dto.getUserIds()) {
            VeStuAssocCharge moralProgUser = new VeStuAssocCharge();
            moralProgUser.setAssocId(dto.getId());
            moralProgUser.setStuId(Integer.parseInt(userId));
            veStuAssocChargeService.insert(moralProgUser);
        }
    }

    @Override
    public void importExcel(List<Map<String, Object>> mapList){
        for (Map map: mapList) {
            VeStuAssocInfoDto dto = new VeStuAssocInfoDto();
                   //         dto.setLogo ((String)map.get(0000));
                   //         dto.setName ((String)map.get(0000));
                   //         dto.setIntro ((String)map.get(0000));
                   //         dto.setStatus ((String)map.get(0000));
                   //         dto.setAddTime ((String)map.get(0000));
            
            super.insert(dto);
        }

    }

}
